package com.xaicode.auth.security;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.collection.CollUtil;
import com.xaicode.auth.service.ISysMenuService;
import com.xaicode.auth.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义检查角色和权限业务
 * <p>
 * 使用 <code>@SaCheckRole</code> 和 <code>@SaCheckPermission</code>
 * 时需指定角色和权限判定方式
 * <p>
 * 本类不使用 asl-common-auth 中的 StpInterfaceImpl
 * 
 * @date 2024/12/6
 */
@Slf4j
@Component
//@ConditionalOnProperty(name = "sa-token-enabled", havingValue = "true")
//@ConditionalOnMissingClass("cn.dev33.satoken.stp.StpInterface")
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    ISysRoleService sysRoleService;
    @Autowired
    ISysMenuService sysMenuService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        log.debug("getPermissionList {} {}", loginId, loginType);
        // 判断是否为系统管理员
        if (LoginUtil.isAdminLogin()) {
            // 系统管理员拥有所有权限
            return CollUtil.newArrayList(AuthConst.ADMIN_PERMISSION);
        }
        return sysMenuService.getMenuPermissionByUserId(loginId.toString());
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        log.debug("getRoleList {} {}", loginId, loginType);
        if (LoginUtil.isAdminLogin()) {
            return CollUtil.newArrayList(AuthConst.ADMIN_ROLE);
        }
        return sysRoleService.getRolePermissionByUserId(loginId.toString());
    }

}
